package com.example.hp.qingyi.compass.fragment.fragment.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.fastjson.JSON;

import com.example.hp.qingyi.R;
import com.example.hp.qingyi.compass.fragment.fragment.adapter.HotspotFragmentAdapter;
import com.example.hp.qingyi.compass.fragment.fragment.base.HotspotBaseData;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.List;

import okhttp3.Call;


/**
 * A simple {@link Fragment} subclass.
 */
public class HotspotFragment extends Fragment {
//    private String url = "http://www.iqingyi.com/recommend/getTopicEditorFeeds?latest_view_post_col_time=1541357113&earliest_view_post_col_time=1538485908";
    private String url = "http://www.iqingyi.com/recommend/getTopicEditorPost?startidx=0&num=20";
    private RecyclerView recyclerView;
    private HotspotFragmentAdapter adapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_hotspot, container, false);
        recyclerView = view.findViewById(R.id.rv_hotspot);
        getHomeData();
        return view;
    }


    /**
     * 联网请求
     */
    private void getHomeData() {
        OkHttpUtils.get().url(url)
                .build().execute(new StringCallback() {


            @Override
            public void onError(Call call, Exception e, int id) {
                Log.i("TagA", "e:" + e.getMessage());
            }

            @Override
            public void onResponse(String response, int id) {
                Log.i("TagA", "response:" + response);
                processData(response);
            }
        });
    }

    private void processData(String response) {
        HotspotBaseData hotspotBaseData = JSON.parseObject(response, HotspotBaseData.class);
        List<HotspotBaseData.DataBean> data = hotspotBaseData.getData();

        if (data != null) {
            //代表有数据
            //设置适配器
            adapter = new HotspotFragmentAdapter(getContext(),data);
            recyclerView.setLayoutManager(new GridLayoutManager(getContext(),1));
            recyclerView.setAdapter(adapter);


        } else {
            //没有数据
        }

    }
}
